package com.hdu.holder;


import com.hdu.result.R;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

import static com.alibaba.fastjson.JSON.parseObject;

@Data
@AllArgsConstructor
@Slf4j
public class BeanAndMethodHolder {
    private Object bean;
    private Method method;


    public R invoke(String taskName, String taskParam) {
        log.info("taskName : {}, taskParam : {}, 开始执行", taskName, taskParam);
        Parameter parameter = method.getParameters()[0];
        Object param = parseObject(taskParam, parameter.getType());
        try {
            return (R) method.invoke(bean, param);
        } catch (IllegalAccessException | InvocationTargetException e) {
            log.error("未知异常, taskName : {}, taskParam : {}", taskName, taskParam);
            return R.fail("未知异常 : " + e.getMessage());
        }
    }
}
